package com.bookcity.controller;

import com.alibaba.fastjson.JSONObject;
import com.bookcity.enums.UserPermission;
import com.bookcity.pojo.User;
import com.bookcity.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author: Trioutia
 * @Date: 2022/5/24 - 05 - 24 - 13:58
 * @Description: com.bookcity.controller
 * @version: 1.0
 */
@Controller
@RequestMapping("/user")
public class RestUserController {

    @Autowired
    @Qualifier("userServiceImpl")
    private UserService usersService;

    //  如果是用户则跳转到info(跳转路径: ${pageContext.request.contextPath }/user/info)
    @RequestMapping("/info")
    public String WrapInfo(){
        return "info";
    }

    //  如果是管理员跳转到onbook(跳转路径: ${pageContext.request.contextPath }/user/onbook)
    @RequestMapping("/onbook")
    public String WrapBook(){
        return "onbook";
    }

    @RequestMapping("/login")
    @ResponseBody
    public String Login(@RequestParam("account") String account,
                        @RequestParam("password") String password){

        User user = usersService.queryUsers(Integer.parseInt(account));    //  找User
        JSONObject resp = new JSONObject();

        if (user != null){  //  若找到了(注册过)
            resp.put("isRegister", true);
            resp.put("checkLogin", password.equals(user.getUpass()));
            resp.put("name", user.getUname());
            resp.put("uid", user.getUid());
            if (user.getRole() == UserPermission.ADMIN.getCode()){  //  如果是管理员就跳转到上架
                resp.put("isUser", false);
            }
            else{
                resp.put("isUser",true);
            }
        }
        else{   // 没注册过
            resp.put("isRegister", false);
        }
        System.out.println("User: "+usersService.getClass().getClassLoader());
        return resp.toJSONString();
    }

    @RequestMapping("/register")
    @ResponseBody
    public synchronized String Register(@RequestParam("pwd") String password,
                                        @RequestParam("phone") String phone,
                                        @RequestParam("perm") String permission){
        JSONObject json = new JSONObject();

        User user = new User("user" + phone, password, phone, UserPermission.valueOf(permission).getCode());  //  将前端数据组合成User类型的数据

        usersService.addUsers( user );  //  入库
        json.put("isRegister", true);

        user = usersService.queryLastOne();
        json.put("uid", user.getUid());

        return json.toJSONString();
    }
    @RequestMapping("/getUser")
    @ResponseBody
    public String getUser(@RequestParam("uid") String uid) {
        User user = usersService.queryUsers(Integer.parseInt(uid));
        JSONObject json = new JSONObject();
        json.put("user", user);
        if(user == null)
            json.put("status", 1);
        else
            json.put("status", 0);
        return json.toJSONString();
    }

}
